#type_file=confidential ; legal_info="Confidential document"
#type_file=internal ; legal_info="Internal document"
type_file=open ; legal_info="Public document"

#cache_dir=../cache/
#dir_name="$(dirname $1)"
cache_dir=./
file_name="$(echo $1 |  sed 's/^\(.*\/\)*\(.*\)/\2/; s/\.md//' )"

# eliminate _ in file name if it starts uppercase
first_char="$(expr substr $file_name 1 1)"
first_char_upper="$(echo "$file_name" | tr '[:lower:]' '[:upper:]')"
first_char_upper="$(expr substr $first_char_upper 1 1)"
if [ "$first_char" = "$first_char_upper" ]; then
    title="$( echo $file_name | tr -s '_' ' ')"
else
    title=$file_name
fi

# Get current date

current_date=$(date | awk '{print $2 " " $3 " " $4}')

# look and append version to the documents

algorithm_version=$( cat $2/code/xrnlib/lib/xrn_settings/inc/xrn_settings.h | awk '/XRN_ALGORITHM_VERSION/ {print $3}' | head -n 1)
io_version=$( cat $2/code/xrnlib/lib/xrn_settings/inc/xrn_settings.h | awk '/XRN_IO_VERSION/ {print $3}' | head -n 1)


curr_dir="$(pwd)"
out_dir="$3"
home_dir="${HOME}"

cat > $cache_dir/args.txt << EOF1
---
title: "$title"
author: []
legalinfo: $legal_info
page-background: "${home_dir}/texmf/tex/$type_file.pdf"
date: "$current_date"
lang: "en"
titlepage: true,
titlepage-background: "${home_dir}/texmf/tex/titlepage.pdf"
titlepage-text-color: "000000"
titlepage-rule-color: "360049"
titlepage-rule-height: 0
...

\vspace*{2cm}
\centerline{
\Large
\textbf{
Xoron Library Version ${algorithm_version}.${io_version}
}
}
\vspace*{2cm}

EOF1

#cp $1 $cache_dir
cat $cache_dir/args.txt $1 > $cache_dir/tmp.md
cd $cache_dir 


# Create pdf
pandoc "tmp.md" -o "$out_dir/pdf/$file_name.pdf" --from markdown --template "/usr/share/pandoc/data/templates/xoronos.tex" --listings --toc 



# Create html
sed -i 's/\.eps/\.svg/g ; s/\](\.\//\](\.\/svg\//g ; s/Return Values\./Return Values/ ;  s/Supported Conversions\./Supported Conversions/ ; s/Arguments\./Arguments/' tmp.md ;
pandoc --standalone "tmp.md" --from markdown --mathjax --toc -o "document_tmp.html" ; 

line=$(grep -n '/style' document_tmp.html | cut -d ":" -f 1)
{ head -n $(($line-1)) document_tmp.html; cat ./xoronos.css; tail -n +$line document_tmp.html; } > document_tmp2.html ;

line2=$(grep -n '<body>' document_tmp2.html | cut -d ":" -f 1)
{ head -n $(($line2-1)) document_tmp2.html; cat ./xoronos_font.html; tail -n +$line2 document_tmp2.html; } > document_tmp3.html ;

cat document_tmp3.html | sed "s/<\/nav>/<\/nav>\n<div>/g; s/<\/body>/<\/div>\n<\/body>/g" > document_tmp4.html

line3=$(grep -n '/body>' document_tmp4.html | cut -d ":" -f 1)
{ head -n $(($line3-1)) document_tmp4.html; cat ./xoronos.js; tail -n +$line3 document_tmp4.html; } > document_tmp5.html ;

cat document_tmp5.html | sed '/<li>.*.<ul>/s/li><a/li><a class="caret"/; /<li>.*.<ul>/s/<ul/<ul class="nested"/; ' > document_tmp6.html ;

cat document_tmp6.html | sed "s/\\\hdots/.../g; s/\\././; s/cline[{][^}]*[}]/\\\\\ \\\hline/ " > $out_dir/html/$file_name.html ; 

rm document_tmp.html document_tmp2.html document_tmp3.html document_tmp4.html document_tmp5.html document_tmp6.html


rm -f tmp.md args.txt


cd $curr_dir 

